﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Draco.OWF.RT.Engine
{
    public class Shell: IDisposable
    {
        private IEngine engine;

        public Shell()
        {
            engine = new DefaultEngine();
            Initialize();
        }

        private void Initialize()
        {
            engine.Initialize();
        }

        public void exec(String arg)
        {
            ShellCommand command = new ShellCommand(arg);
            if(String.IsNullOrWhiteSpace(command.Command)){
                Console.WriteLine("Please input command!");
                return;
            }
            String cmd = command.Command.ToLower();
            switch (cmd)
            {
                case ShellCommand.CMD_HELP:
                    {
                        ShowHelp(command);
                        break;
                    }
                case ShellCommand.CMD_VERSION:
                    {
                        ShowVersion(command);
                        break;
                    }
                case ShellCommand.CMD_LOAD_PROCESS:
                    {
                        LoadProcess(command);
                        break;
                    }
                default:
                    {
                        Console.WriteLine(String.Format("unknow command:{0}", command.Command));
                        break;
                    }
            }
        }

        private void ShowHelp(ShellCommand command)
        {
            Console.WriteLine(String.Format("This is Draco OWF engine Help", command.Command));
        }
        private void ShowVersion(ShellCommand command)
        {
            Console.WriteLine(String.Format("This is Draco OWF engine", command.Command));
        }
        private void LoadProcess(ShellCommand command)
        {
            String file = command.DefaultParameter;
            if (String.IsNullOrWhiteSpace(file) || !File.Exists(file))
            {
                Console.WriteLine(String.Format("ERROR:File {0} doesn't exist", file));
                return;
            }
            IProcess process = engine.LoadProcess(file);
            Console.WriteLine(String.Format("Process {0} Loaded",process==null?"": process.Name));
        }


        public void Dispose()
        {
            engine.Dispose();
        }
    }
}
